package com.dhms.vostok1.adapter

import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.databinding.BindingAdapter
import com.dhms.basebusiness.R
import com.dhms.uikit.AlertBannerType
import com.dhms.uikit.AlertBannerView
import com.dhms.vostok1.data.spot_patrol.TaskStatus
import com.dhms.vostok1.model.OfflineModeState
import com.dhms.vostok1.utils.Utils

@BindingAdapter("itemTextColor")
fun bindItemTextColor(textView: TextView, color: Int?) {
    color?.let {
        textView.apply {
            setTextColor(ContextCompat.getColor(context, it))
        }
    }
}

@BindingAdapter("taskEndTime", "status")
fun bindingTaskTag(layout: ConstraintLayout, taskEndTime: String?, status: TaskStatus?) {
    val timeout = Utils.parseTimeout(taskEndTime ?: "")

    (layout.children.last() as TextView).text =
        layout.context.getString(R.string.end_timeout_with_title, timeout)

    if (status == TaskStatus.DOING && timeout != -1) {
        layout.visibility = View.VISIBLE
    } else {
        layout.visibility = View.GONE
    }
}

@BindingAdapter("taskEndTime", "status")
fun bindingTaskTimeout(textView: TextView, taskEndTime: String?, status: TaskStatus?) {
    val timeout = Utils.parseTimeout(taskEndTime ?: "")
    textView.text =
        textView.context.getString(R.string.end_timeout_with_title, timeout)
    textView.visibility =
        if (status == TaskStatus.DOING && timeout != -1) View.VISIBLE else View.GONE
}

@BindingAdapter("taskEndTime", "status")
fun bindingInfoTimeoutItem(linearLayout: LinearLayout, taskEndTime: String?, status: TaskStatus?) {
    val timeout = Utils.parseTimeout(taskEndTime ?: "")
    (linearLayout.getChildAt(1) as TextView).text =
        linearLayout.context.getString(R.string.end_timeout, timeout)
    linearLayout.visibility =
        if (status == TaskStatus.DOING && timeout != -1) View.VISIBLE else View.GONE
}

@BindingAdapter("state", "syncDataContent")
fun bindAlertBannerOfflineModeState(
    alertBannerView: AlertBannerView,
    state: OfflineModeState,
    syncDataContent: String
) {
    alertBannerView.apply {
        this.binding.progressBarVisibility = 0
        var icon: Int? = null
        var messageResId: Int? = null
        var bannerType = AlertBannerType.MESSAGE
        when (state) {
            OfflineModeState.OFFLINE -> {
                icon = R.drawable.ic_offline_mode
                messageResId = R.string.offline_mode
            }
            OfflineModeState.SYNCHRONIZING -> {
                this.binding.progressBarVisibility = 1
                messageResId = R.string.data_synchronization
            }
            OfflineModeState.COMPLETE -> {
                icon = R.drawable.ic_offline_mode_complete
                messageResId = R.string.sync_complete
            }
            OfflineModeState.ERROR -> {
                icon = R.drawable.ic_offline_mode_waring
                messageResId = R.string.sync_exist_exception
                bannerType = AlertBannerType.ERROR
            }
            else -> {}
        }
        if (icon != null) {
            this.binding.icon = ContextCompat.getDrawable(
                context,
                icon
            )
        } else {
            this.binding.icon = null
        }
        messageResId?.let {
            var content = context.getString(it)
            if (OfflineModeState.SYNCHRONIZING == state) {
                content += syncDataContent
            }
            this.binding.message = content
        }
        this.setBannerType(bannerType)
    }
}